﻿using System;
using System.Text;

namespace MxitAPI {

	public class MessageTextTokeniser {

        private StringBuilder messageText;
        private int index;

        private bool bold;
        private bool italic;
        private bool underline;
        private bool link;
        private int size;
        StringBuilder text;

        public MessageTextTokeniser( string messageText ) {
            this.messageText = new StringBuilder( messageText );
            index = 0;
            bold = false;
            italic = false;
            underline = false;
            link = false;
            size = 0;
        }

        public bool HasNext() {
            if ( index < messageText.Length )
                return true;
            return false;
        }

        private TextMessageTextToken CreateToken() {
            TextMessageTextToken token = new TextMessageTextToken();
            token.Text = text.ToString();
            token.TextProperties = TextProperties.None;

            if ( !link ) {
                token.TextSize = size;
                if ( bold )
                    token.TextProperties |= TextProperties.Bold;
                if ( italic )
                    token.TextProperties |= TextProperties.Italic;
                if ( underline )
                    token.TextProperties |= TextProperties.Underline;
            }
            else {
                token.Link = true;
            }

            return token;
        }

        public TextMessageTextToken Next() {

            TextMessageTextToken token = null;
            text = new StringBuilder();

            // Go through the text until a token has been created
            bool first = true;
            while ( token == null ) {
                char c = messageText[ index++ ];
                bool appendChar = true;

                if ( c == '$' ) {
                    if ( !first ) {
                        token = CreateToken();
                    }
                    link = !link;
                }

                if ( !link ) {
                    switch ( c ) {
                        case '.':
                            if ( messageText.Length > index ) {
                                if ( messageText[index] == '+' ) {
                                    appendChar = false;
                                    ++index;
                                    ++size;
                                }
                                else if ( messageText[index] == '-' ) {
                                    appendChar = false;
                                    ++index;
                                    --size;
                                }
                            }
                            break;
                        case ' ':
                            if ( size != 0 ) {
                                text.Append( c );
                                token = CreateToken();
                                size = 0;
                            }
                            break;
                        case '*':
                            appendChar = false;
                            if ( !first ) {
                                token = CreateToken();
                            }
                            if ( bold )
                                bold = false;
                            else
                                bold = true;
                            break;
                        case '/':
                            appendChar = false;
                            if ( !first ) {
                                token = CreateToken();
                            }
                            if ( italic )
                                italic = false;
                            else
                                italic = true;
                            break;
                        case '_':
                            appendChar = false;
                            if ( !first ) {
                                token = CreateToken();
                            }
                            if ( underline )
                                underline = false;
                            else
                                underline = true;
                            break;

                        /*default:
                        break;     */
                    }
                }

                if ( appendChar ) {
                    text.Append( c );
                }

                // If the end of teh string has been reached, create a token
                if ( token == null && index == messageText.Length ) {
                    token = CreateToken();
                }

                first = false;
            }

   
            return token;
        }

	}


}
